package com.vvlive.caibao.smartcity.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vvlive.caibao.smartcity.app.SmartCityApp;
import com.vvlive.caibao.smartcity.base.StringHttpCallBack;
import com.vvlive.caibao.smartcity.bean.User;
import com.vvlive.caibao.smartcity.utils.MeetingsController;
import com.vvlive.caibao.smartcity.utils.SettingsUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.BindView;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by android on 2015/7/3.
 * 登录界面
 */
public class SignInActivity extends KJActivity {

    /**
     * 登录成功的RESULT_CODE
     */
    public static final int LOGIN_RESULT_CODE = 2020;

    @BindView(id = R.id.face_icon)
    private ImageView mFaceIcon;
    @BindView(id = R.id.name)
    private TextView mName;
    @BindView(id = R.id.username)
    private EditText mUsername;
    @BindView(id = R.id.pass)
    private EditText mPassword;
    @BindView(id = R.id.sign_in, click = true)
    private ImageButton mSignIn;
    @BindView(id = R.id.forgot_pass, click = true)
    private TextView mForgotPassword;
    @BindView(id = R.id.create_account, click = true)
    private TextView mCreateAccount;

    private CommonActionBar mCommonActionBar ;

    private Context mContext;

    private MeetingsController mController;
    private ProgressDialog mProgress;
    private SettingsUtil mSettings;

    @Override
    public void setRootView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_signin);
        mContext = this;
        mCommonActionBar = (CommonActionBar) findViewById(R.id.signin_common_actionbar);
        mCommonActionBar.setBackIcon(R.mipmap.back);
        mCommonActionBar.setMoreVisible(false);
        mCommonActionBar.setTitle(R.string.login);

        mSettings = new SettingsUtil(this);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.forgot_pass:  // 忘记密码
                showActivity(this, ForgotPassWord.class);
                break;
            case R.id.create_account:   // 创建账户
                showActivity(this, CreateAccountAtivity.class);
                break;
            case R.id.sign_in:  // 登录
                if (canSingIn()) {
                    login();
                } else {
                    Toast.makeText(this, "请将登录信息填写完整！", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }

    /**
     * 登录
     */
    private void login() {

        String userid = mUsername.getText().toString();
        String password = mPassword.getText().toString();

        if (mController == null) {
            mController = new MeetingsController();
        }

        mProgress = ProgressDialog.show(this, "", getResources().getString(R.string.logining));
        mController.login(userid, password, new LoginCallBack(userid, password));
    }

    /**
     * 登录的回调函数
     */
    class LoginCallBack extends StringHttpCallBack {

        private String userid;
        private String password;

        public LoginCallBack(String userid, String password) {
            this.userid = userid;
            this.password = password;
        }

        @Override
        public void onSuccess(String data) {
            if ("USER_OK".equals(data)) {
                mProgress.dismiss();
                Toast.makeText(mContext, R.string.login_success, Toast.LENGTH_SHORT).show();
                SmartCityApp.isLogin = true;
                User user = new User();
                user.setUsername(userid);
                user.setObjectId(password);
                SmartCityApp.currentUser = user;
                mSettings.saveUser(userid, password);
                setResult(LOGIN_RESULT_CODE);
                finish();
            } else {
                mProgress.dismiss();
                Toast.makeText(mContext, R.string.login_user_error, Toast.LENGTH_SHORT).show();
            }
            Log.i("LOGIN", "data = " + data);
        }

        @Override
        public void onError(int errNo, String str) {
            mProgress.dismiss();
            Toast.makeText(mContext, R.string.login_network_error, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 当前的输入条件能否登录
     *
     * @return
     */
    private boolean canSingIn() {
        return !TextUtils.isEmpty(mUsername.getText().toString()) &&
                       !TextUtils.isEmpty(mPassword.getText().toString());
    }


}
